// pages/invite/invite.js
const app = getApp()
const util = require('../../utils/util.js')
const Debug = require('../../utils/Debug.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
        page:1,//默认第一页
        youshow: false,//没有好友
        tabtitle:["邀请好友","好友列表"],
        num:0,//默认邀请好友
          sharebox: ["https://img-1256481299.cos.ap-guangzhou.myqcloud.com/qqhxcx/yqhy.jpg", "https://xq.shzxcx.com/static/image/xq/share2.jpg"],
        list:[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
          this.setData({
                  headerheight: app.globalData.navHeight
          })
  },
        tabbut:function(e){
                this.setData({
                        num: e.currentTarget.dataset.num,
                        page: 1,
                        list: []
                })
                if (e.currentTarget.dataset.num==1){
                     
                        this.request()
                }
        },
  request:function(){
          wx.showLoading({
                  title: '玩命加载中...',
          })
          var data = {
                        user_id: wx.getStorageSync("userInfo").id,
                        pageIndex: this.data.page,
                        pageSize: 10
                        }
          var header = {
                  'authorization': wx.getStorageSync("tekon"),
                  'content-type': "application/x-www-form-urlencoded"
          }
          util.formhttp(app.globalData.appurl + "/goldconins/promote-give-list", 'get', data, header, this.requestdata);
        },
        requestdata:function(res){
                Debug.debug("<-------好友列表promote-give-list------->")
                Debug.debug(res)
                    if (res.status == 401){
                            this.login()
                        }
                //数据处理
                wx.hideLoading()
                if (res.data.list.length==0){
                        if (this.data.page== 1) {
                                this.setData({
                                        youshow: true,
                                })
                        }
                }
                var list = [];
                var totalList = [];
                for (var ids in res.data.list) {
                        var detail = res.data.list[ids];
                        var temp = {
                                username: detail.userName,
                                coins: detail.conis,
                                givecoins: detail.give_coins
                        }
                        list.push(temp)
                }
                totalList = this.data.list.concat(list);
                this.setData({
                        list: totalList,
                })
        },
        // 登录
        login: function () {
                var that = this
                wx.login({
                        success: function (res) {
                                var code = res.code
                                // console.log(that.data.channel)
                                var data = {
                                        userCode: code,
                                        channel: that.data.channel,
                                        userImageUrl: "",
                                        userName: ""
                                }
                                util.promotion(app.globalData.appurl + "/qinqiang/user", "POST", data, that.succeed)
                        }
                })
        },
        succeed: function (res) {
                Debug.debug(res)
                // console.log(res)
                wx.setStorageSync('tekon', res.data.token)
                wx.setStorageSync('userInfo', res.data.user)
                this.request()
        },
  /**
   * 页面上拉触底事件的处理函数
   */
        onReachBottom: function () {
                var that = this
                if (that.data.num==1){
                        var pages = that.data.pages
                        pages++
                        that.setData({
                                pages: pages
                        })
                        that.request()
                }
        },
  /**
   * 用户点击右上角分享
   */
        onShareAppMessage: function (res) {
                var that = this
                if (wx.getStorageSync("userInfo").userName == "") {
                        if (res.from === 'button') {
                                // 来自页面内转发按钮
                        }
                        return {
                                title: '秦腔小妹邀请你观看西北秦腔汇，一起挣金币，快来吧~',
                                imageUrl: that.data.sharebox[Math.floor(Math.random() * 2)],
                                path: '/pages/index/index?&user_id=' + wx.getStorageSync("userInfo").id,
                        }
                } else {
                        if (res.from === 'button') {
                                // 来自页面内转发按钮
                        }
                        return {
                                title: wx.getStorageSync("userInfo").userName + '邀请你观看西北秦腔汇，一起挣金币，快来吧~',
                                imageUrl: that.data.sharebox[Math.floor(Math.random() * 2)],
                                path: '/pages/index/index?&user_id=' + wx.getStorageSync("userInfo").id,

                        }
                }

        }
})